﻿<script src="@Url.Content("~/Scripts/js/iconselector.js")"></script>
<body style="padding: 2px; height: 100%; text-align: center;">
    <div id="layout">
        <ipnut type="hidden" id="MenuNo" value="Button" />
        <div position="center" title="系统权限维护">
            <form id="mainform">
                <div id="maingrid" style="margin: 2px;">
                </div>
            </form>
        </div>
    </div>
    <ul class="iconlist">
    </ul>
    <script type="text/javascript">
        var IsAdd = false;
        var selectCombox;
        var selectController;
        //验证
        var maingform = $("#mainform");
        $.metadata.setType("attr", "validate");
        iTools.validate(maingform, { debug: true });
        //覆盖本页面grid的loading效果
        iTools.overrideGridLoading();

        function toolbarBtnItemClick(item) {
            var editingrow = grid.getEditingRow();
            switch (item.id) {
                case "Add":
                    if (editingrow == null) {
                        addNewRow();
                    } else {
                        iTools.tip('请先提交或取消修改');
                    }
                    break;
                case "Update":
                    if (editingrow == null) {
                        var row = grid.getSelectedRow();
                        if (!row) { iTools.tip('请选择行'); return; }
                        grid.beginEdit(row);
                    } else {
                        iTools.tip('请先提交或取消修改');
                    }
                    break;
                case "Save":
                    if (editingrow != null) {
                        grid.endEdit(editingrow);
                    } else {
                        iTools.tip('现在不在编辑状态!');
                    }
                    break;
                case "Cancel":
                    if (editingrow != null) {
                        grid.cancelEdit(editingrow);
                    } else {
                        iTools.tip('现在不在编辑状态!');
                    }
                    break;
                case "Delete":
                    $.ligerDialog.confirm('确定删除吗(该删除操作只是软删除)?', function (confirm) {
                        if (confirm) {
                            var selected = grid.getSelected();
                            if (selected) {
                                if (!selected.PID) {
                                    grid.deleteRow(selected);
                                    return;
                                }
                                var data = $.extend({}, selected);
                                data.PID = selected.PID;
                                $.ajax({
                                    url: '/Button/Delete',
                                    loading: '正在删除中...',
                                    data: data,
                                    success: function () {
                                        iTools.tip('删除成功!');
                                        grid.loadData();
                                    },
                                    error: function (message) {
                                        iTools.showError('删除失败!');
                                    }
                                });
                            }
                            else {
                                iTools.tip('请选择行!');
                            }
                        }
                    });
                    break;
                case "RealDelete":
                    $.ligerDialog.confirm('确定删除吗(该删除操作将会永久删除,一旦删除将无法挽回)?', function (confirm) {
                        if (confirm) {
                            var selected = grid.getSelected();
                            if (selected) {
                                if (!selected.PID) {
                                    grid.deleteRow(selected);
                                    return;
                                }
                                var data = $.extend({}, selected);
                                data.PID = selected.PID;
                                $.ajax({
                                    url: '/Button/RealDelete',
                                    loading: '正在删除中...',
                                    data: data,
                                    success: function () {
                                        iTools.tip('删除成功!');
                                        grid.loadData();
                                    },
                                    error: function (message) {
                                        iTools.showError('删除失败!');
                                    }
                                });
                            }
                            else {
                                iTools.tip('请选择行!');
                            }
                        }
                    });
                    break;
            }
        }

        var layout = $("#layout").ligerLayout({ leftWidth: 140 });

        var grid = $("#maingrid").ligerGrid({

            columns: [
                { display: '控制器', name: 'PermissionController', isSort: false, align: 'left', width: 150, minWidth: 60 },
                {
                    display: '动作', name: 'PermissionAction', isSort: false, align: 'left', width: 200, minWidth: 60, validate: { required: true }, editor: {
                        type: 'select', ext: function (rowdata) {
                            return {
                                onBeforeOpen: function () {
                                    selectCombox = this;

                                    $.ligerDialog.open({
                                        title: '选择动作', name: 'winselector', width: 1100, height: 400, url: '/Button/SelectAction', buttons: [
                                              {
                                                  text: '确定', onclick: function (item, dialog) {
                                                      var fn = dialog.frame.f_select || dialog.frame.window.f_select;
                                                      var data = fn();
                                                      if (!data) {
                                                          alert('请选择行!');
                                                          return;
                                                      }
                                                      var editingrow = grid.getEditingRow();
                                                      if (editingrow) {
                                                          if (selectCombox) {
                                                              selectCombox.inputText.val(data.ActionName);
                                                              selectCombox.valueField.val(data.ActionName);
                                                              selectController = data.ControllerName;
                                                          }
                                                      }

                                                      dialog.close();
                                                  }
                                              },
                                              { text: '取消', onclick: function (item, dialog) { dialog.close() } }
                                        ]
                                    });

                                    return false;
                                },
                                render: function () {
                                    return rowdata.PermissionAction;
                                }
                            };
                        }
                    }, render: function (item) {
                        return item.PermissionAction;
                    }
                },
                { display: '权限名称', name: 'PermissionName', align: 'left', width: 150, minWidth: 60, validate: { required: true }, editor: { type: 'text' } },
                { display: 'PID', name: 'PID', align: 'left', width: 100, minWidth: 60, hide: true },
                { display: 'ParentID', name: 'ParentID', align: 'left', width: 100, minWidth: 60, hide: true },
                {
                    display: '图标', name: 'Icon', isSort: false, align: 'left', width: 50, minWidth: 50, editor: {
                        type: 'select', ext: function (rowdata) {
                            return {
                                onBeforeOpen: function () {
                                    currentComboBox = this;
                                    f_openIconsWin();
                                    return false;
                                },
                                render: function () {
                                    return rowdata.Icon;
                                }
                            };
                        }
                    }
                  , render: function (item) {
                      return "<div style='width:100%;height:100%;'><img src='" + item.Icon + "' /></div>";
                  }
                },
                { display: '是否为按钮', name: 'IsButton', align: 'left', width: 80, minWidth: 60, validate: { required: true }, editor: { type: 'checkbox' }, render: function (item) { if (item.IsButton) return "按钮"; else return "普通请求"; } },
                {
                    display: '是否可见', name: 'IsVisible', align: 'left', width: 60, minWidth: 60, validate: { required: true }, editor: { type: 'checkbox' }, render: function (item) {
                        if (item.IsVisible)
                            return "可见";
                        else
                            return "隐藏";
                    }
                },
                { display: '排序值', name: 'Sort', align: 'left', width: 40, minWidth: 20, validate: { number: true }, editor: { type: 'numberbox' } },
                { display: '描述', name: 'Description', align: 'left', width: 200, minWidth: 60, editor: { type: 'text' } }

            ], dataAction: 'server', pageSize: 10, toolbar: {}, sortName: 'PermissionController', url: '/Button/GetPermissionForGrid',
            width: '98%', height: '100%', heightDiff: -5, checkbox: false, usePager: false, enabledEdit: true, clickToEdit: false, tree: { columnName: 'PermissionAction' },
            fixedCellHeight: true, rowHeight: 25
        });

        //本地按钮
        var toolbarOptions = [
               { text: '保存', id: 'Save', click: toolbarBtnItemClick, img: "@Url.Content("~/Content/icons/silkicons/page_save.png")" },
                  { text: '取消', id: 'Cancel', click: toolbarBtnItemClick, img: "@Url.Content("~/Content/icons/silkicons/cancel.png")" }
        ];
        //加载toolbar
        iTools.loadToolbar(grid, toolbarBtnItemClick, toolbarOptions);

        grid.bind('beforeSubmitEdit', function (e) {
            if (!iTools.validator.form()) {
                iTools.showInvalid();
                return false;
            }
            return true;
        });
        grid.bind('afterSubmitEdit', function (e) {
            var isAddNew = e.record['__status'] == "add";
            var data = e.newdata;
            if (!selectController) {
                selectController = e.record.PermissionController;
            }
            data.PermissionController = selectController;
            data.ParentID = e.record.ParentID ? e.record.ParentID : 0;
            data.IsButton = data.IsButton == 1;
            data.IsVisible = data.IsVisible == 1;
            if (!isAddNew) {
                data.PID = e.record.PID;
            }
            $.ajax({
                loading: '正在保存数据中...',
                url: '/Button/' + (isAddNew ? "Add" : "Update"),
                data: data,
                success: function () {
                    grid.loadData();
                    iTools.tip('保存成功!');
                },
                error: function (message) {
                    iTools.tip(message);
                }
            });
        });

        function addNewRow() {
            var selectRow = grid.getSelectedRow();
            if (!selectRow) {
                //添加根节点
                grid.addEditRow();
                return true;
            }

            //如果是叶子节点
            if (grid.isLeaf(selectRow))
                grid.upgrade(selectRow);
            //添加到该节点下
            var data = {};
            data.ParentID = selectRow.PID;
            var newData = grid.add(data, selectRow, false, selectRow);
            grid.beginEdit(newData);
        }
    </script>
</body>
